Page({
  data: {
    items: [
      { name: 'qq', value: 'QQ', checked: false },
      { name: 'wechat', value: '微信', checked: false},
      { name: 'phone_num', value: '电话号码', checked: false },
    ],
    photo_data: {},
    form_data: {},
  },

  onLoad() {},
  // 上传照片
  chooseImage() {
    my.chooseImage({
      sourceType: ['camera','album'],
      count: 9,
      success: (res) => {
     
        this.setData({
          photo_data: JSON.stringify(res),
        });

        console.log(this.data.photo_data);
        const apiUrl = 'http://172.20.55.210:10086/product/photo'; // 替换成你的后端接口地址
        my.request({
          url: apiUrl,
          method: 'POST',
          data: this.data.photo_data,
          dataType: 'json',
          success: resp => {
            console.log(resp.data);
            goodsList = resp.data;
            my.alert({ content: 'success' });
          },
          fail: error => {
            console.error('请求失败：', error);
            my.alert({ content: '请求失败，详细错误信息请查看控制台' });
          },
        });
      },
      fail:()=>{
        my.showToast({
          content: '上传失败', // 文字内容
        });
      }
    })
  },
  onSubmit(e) {
    // my.alert({
    //   content: `数据：${JSON.stringify(e.detail.value)}`,
    // });
    this.setData({
      form_data: JSON.stringify(e.detail.value),
    });

    // console.log('123', this.data.photo_data);
    // console.log(this.data.form_data);
    
    this.requestToBackend_product_form_list(this.data.form_data);
  },

  // 选择联系方式后生成输入框
  onChange(e) {
    const checked = e.detail.value;
    // console.log('zhixingzhong');   

    if(e.detail.value.length == 0){
      for(let j = 0; j < this.data.items.length; j++)
      {
        this.setData({
          [`items[${j}].checked`]: false,
        });
      }
    }

    for(let i = 0; i < e.detail.value.length; i++)
    {
      for(let j = 0; j < this.data.items.length; j++)
      {
        let flag = 0;
        console.log(this.data.items[j].name, e.detail.value);
        if(this.data.items[j].name == e.detail.value[i])
        {
          this.setData({
            [`items[${j}].checked`]: true,
          });
        }
        else if(this.data.items[j].name != e.detail.value[i])
        {
          for(let k = 0; k < e.detail.value.length; k++)
          {
            if(this.data.items[j].name == e.detail.value[k])
            {
              this.setData({
                [`items[${j}].checked`]: true,
              });
              flag = 1;
            }
          }
          if(flag == 0)
          {
            this.setData({
              [`items[${j}].checked`]: false,
            });
          }
        } 
      }
    }
  },

  // product_form_list
  requestToBackend_product_form_list(data){
    const that = this;
    // const apiUrl = 'https://localhost:3000/php/1.php'; // 替换成你的后端接口地址
    // const apiUrl = 'http://172.20.55.210:10086/product/getproducts'; // 替换成你的后端接口地址
    const apiUrl = 'http://172.20.55.210:10086/product'; // 替换成你的后端接口地址

    console.log(data.detail.value); 
    // console.log('正在传向后端');

    my.request({
      url: apiUrl,
      method: 'POST',
      data: data.detail.value,
      dataType: 'json',
      success: resp => {
        console.log(resp.data);
        goodsList = resp.data;
        my.alert({ content: 'success' });
      },
      fail: error => {
        console.error('请求失败：', error);
        my.alert({ content: '请求失败，详细错误信息请查看控制台' });
      },
    });
  }
});
